<script setup lang="ts">
  import { ref, defineEmits, watch, onMounted, PropType } from 'vue';
  import { useBooleanStore } from '@/store';
  import { getCarInventoryPage } from '@/api/les';
  import { selectedOption } from '@/api/system-basic/types';

  const props = defineProps({
    typeOptions: Array as PropType<selectedOption[]>,
  });
  const emit = defineEmits(['updateArray']);
  const booleanStore = useBooleanStore();
  // 表单查询数据
  const carCode = ref('');
  const carType = ref<number[]>([]);
  const carStation = ref('');

  const pageSize = ref(10);
  const pageNumber = ref(1);

  // 表格数据
  async function handleSearch() {
    if (carType.value.length === 0) {
      carType.value = [1, 2];
    }
    const newData = await getCarInventoryPage({
      carCode: carCode.value,
      carType: carType.value,
      carStation: carStation.value,
      current: pageNumber.value,
      size: pageSize.value,
    });
    emit('updateArray', newData);
  }
  // 重置
  function reset() {
    carCode.value = '';
    carType.value = [];
    carStation.value = '';
    handleSearch();
  }
  onMounted(() => {
    handleSearch();
  });
  // 监听分页
  watch([() => booleanStore.pageNum, () => booleanStore.pageSize], () => {
    if (booleanStore.num5 === 55) {
      pageNumber.value = booleanStore.pageNum;
      pageSize.value = booleanStore.pageSize;
      handleSearch();
    }
  });
  watch(
    () => booleanStore.update,
    () => {
      if (booleanStore.num6 === 29) handleSearch();
    }
  );
</script>

<template>
  <a-row :gutter="{ md: 8, lg: 24, xl: 32 }">
    <a-col :span="6">
      <div>
        <a-input v-model="carCode" placeholder="车辆编码" size="large" />
      </div>
    </a-col>
    <a-col :span="6">
      <div>
        <a-select
          v-model="carType"
          multiple
          placeholder="车辆类型"
          :options="props.typeOptions"
        />
      </div>
    </a-col>
    <a-col :span="6">
      <div>
        <a-input v-model="carStation" placeholder="车辆地址" size="large" />
      </div>
    </a-col>
  </a-row>

  <div class="btn-group">
    <a-button type="primary" size="large" @click="handleSearch">查询</a-button>
    <a-button class="btn-reset" size="large" @click="reset">重置</a-button>
    <a-button
      type="primary"
      class="btn-reset"
      size="large"
      @click="booleanStore.setIsOpen('A')"
      >新增</a-button
    >
  </div>
</template>

<style scoped>
  .btn-group {
    float: right;
    margin-top: 15px;
  }

  .btn-reset {
    margin-left: 10px;
  }
</style>
